var keyword:String;

var memberPageNum:Number;
var memberPageTotal:Number;

var membersResult:Object;

function init()
{
	Packages.model.Context.activeContent=this;

	// UI.init
	var basic=Packages.system.mlf.GateKeeper.scanElement(this._name, "basic");
	Packages.system.display.JAccordion.initialize(basic);

	// Render.init
	Packages.system.render.Filter.titleFilter(Packages.system.mlf.GateKeeper.scanElement(this._name, "memberListTitle"));
	Packages.system.render.Filter.titleFilter(Packages.system.mlf.GateKeeper.scanElement(this._name, "friendsPageTitle"));
	Packages.system.display.SceneTransition.rollOut();

	// Event.init
	var evtTargetArr:Array=new Array();
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "back_print"));
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "memberListPreSpot"));
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "memberListNextSpot"));
	Packages.system.events.StateFulButtonEvent.initialize(evtTargetArr);

	invokeInitComplete();
}

function dispose()
{
	Packages.system.mlf.GateKeeper.removeListener("onReleaseButtonEvent", this);

	delete keyword;
	delete memberPageTotal;
	delete memberPageNum;
	delete membersResult;
}

function invokeInitComplete()
{
	Packages.system.mlf.GateKeeper.addListener("onReleaseButtonEvent", this);

	// keyword
	keyword=Packages.model.Context.address.keyword;

	memberPageNum=1;
	memberPageTotal=1;
	var memberListCurrentPage=Packages.system.mlf.GateKeeper.scanElement(this._name, "memberListCurrentPage");
	memberListCurrentPage.text=memberPageNum;
	var tf_fmt:TextFormat=new TextFormat();
	tf_fmt.align="right";
	memberListCurrentPage.setTextFormat(tf_fmt);

	searchMemberList();
}

function searchMemberList(param:String, data:Object)
{
	var method="members/search/"+escape(keyword);

	//
	if (data.pageNum==null)
	{
		memberPageNum=1;
	}
	else memberPageNum=data.pageNum;

	startIndex=(memberPageNum-1)*8;
	
	Packages.modula.RequestCommand.request("HTTP", "restlet/"+method+"?startIndex="+startIndex+"&count=8", null, Packages.model.vo.UserListView, "detail", this, {container:"memberListContainer", slot:this._name, uri:"app/content/MemberList.xml", saved:"membersResult", initMethod:"lib:notes.components.content.MemberList.onMembersContainerInit"});
}

function onMembersContainerInit(param, data, slot)
{
	// UI.init
	var memberListContainer=Packages.system.mlf.GateKeeper.scanElement(this._name, "memberListContainer");
	Packages.system.display.JAccordion.initialize(memberListContainer, true);

	var rstCollection=membersResult.objects;	// @tmp ..
	var processMemberId;
	var coverContainer;
	for (var i=0; i<rstCollection.length; i++)
	{
		processMemberId=rstCollection[i].memberId;
		coverContainer=Packages.system.mlf.GateKeeper.scanElement(this._name, "picCover_"+processMemberId);
		var listener:Object=new Object();
		listener.onLoadInit=function(target_mc)
		{
			var minWidth:Number=80;
			var minHeight:Number=80;
			var zoom=Packages.system.utils.ImageUtil.minSize(target_mc, minWidth, minHeight);
			target_mc._xscale=zoom;
			target_mc._yscale=zoom;
			// centralize the target
			var offsetX:Number=(target_mc._width-minWidth)/2;
			target_mc._x=-offsetX;
			var offsetY:Number=(target_mc._height-minHeight)/2;
			target_mc._y=-offsetY;
		}
		if (rstCollection[i].image!=null)
		{
			mlf.mcmanager.dynamicloading.LoadApp.loadAppLoader(rstCollection[i].image, coverContainer, "img", listener); 
		}
	}
}

function previousBookList()
{
	if (memberPageNum>1)
	{
		memberPageNum--;
		searchMemberList(null, {pageNum:memberPageNum});
	}
}

function nextBookList()
{
	if (memberPageNum<memberPageTotal)
	{
		memberPageNum++;
		searchMemberList(null, {pageNum:memberPageNum});
	}
}

function onReleaseButtonEvent(evt:Object)
{
	if (evt.param=="memberListPreSpot")
	{
		previousBookList();
	}
	else if (evt.param=="memberListNextSpot")
	{
		nextBookList();
	}
	else if (evt.param.indexOf("memberItem")>-1)
	{
		var selectedMemberId=evt.param.substr(11);
		Packages.system.utils.ComponentsUtil.popupSelectedDataDetail(membersResult, selectedMemberId, "MemberDetail", "content/myReadjoy/PersonPage");
	}
}

function handleResponseResult(rsp:Object)
{
	if (rsp.vo.instance.type=="Template")
	{
		Packages.system.mlf.GateKeeper.loadML2byContent(rsp.vo.instance.content);
	}

	if (rsp.component.data.saved!=null)
	{
		this[rsp.component.data.saved]=rsp.service.result;
	}
}